"use client"; // 必须要添加
import {  ReactElement, createContext, useContext } from "react";
import {useLocalObservable,enableStaticRendering} from 'mobx-react-lite'
import createStore, { IStore } from "./rootStore";

interface IProps {
    initialValue: Record<any,any>
    children: ReactElement
}

enableStaticRendering(!process.browser)

const StoreContext = createContext({})

export const StoreProvider = ({ initialValue, children }: IProps) =>{
    const store: IStore = useLocalObservable(createStore(initialValue))
    return (
        <StoreContext.Provider value={store}>{children}</StoreContext.Provider>
    )
}

export const useStore = () =>{
    const store: IStore = useContext(StoreContext) as IStore
    if(!store){
        throw new Error("数据不存在");
    }
    return store
}